//@author A0094511L
#include "OutputManager.h"

const QString OutputManager::SECTION_HEADER_TITLE_FRONT_TAG = "<table width = \"100%\"><tr><td align = \"left\" width = \"60%\"><span style=\" font-size:15pt; font-weight:600;\">";
const QString OutputManager::SECTION_HEADER_TITLE_BACK_TAG = "</td></span>";
const QString OutputManager::SECTION_HEADER_DATE_FRONT_TAG = "<td align = \"right\"><span style=\" font-size:15pt; font-weight:600;\">";
const QString OutputManager::SECTION_HEADER_DATE_BACK_TAG = "</td></span></tr></table><br>";
const QString OutputManager::SECTION_HEADER_ONLYONE_BACK_TAG = "</tr></table><br>";

const QString OutputManager::OVERDUE_HEADER_TITLE_FRONT_TAG = "<table width = \"100%\"><tr><td align = \"left\" width = \"60%\"><span style=\" font-size:15pt; font-weight:600; color: red;\">";
const QString OutputManager::OVERDUE_HEADER_TITLE_BACK_TAG = "</td></span></tr></table><br>";

const QString OutputManager::TODAY_HEADER_TITLE_FRONT_TAG = "<a id = \"today\"><table width = \"100%\"><tr><td align = \"left\" width = \"60%\"><span style=\" font-size:15pt; font-weight:600; color: green;\">";
const QString OutputManager::TODAY_HEADER_TITLE_BACK_TAG = "</td></span>";
const QString OutputManager::TODAY_HEADER_DATE_FRONT_TAG = "<td align = \"right\"><span style=\" font-size:15pt; font-weight:600; color: green;\">";
const QString OutputManager::TODAY_HEADER_DATE_BACK_TAG = "</td></span></tr></table><br></a>";

const QString OutputManager::SECTION_COMPONENT_NAME_FRONT_TAG_NORMAL = "<table width = \"100%\"><tr><td align = \"left\" width = \"60%\"><span style=\" font-size:10pt; font-weight:600;\">";
const QString OutputManager::SECTION_COMPONENT_NAME_BACK_TAG_NORMAL = "</td></span>";
const QString OutputManager::SECTION_COMPONENT_DATEORTIME_FRONT_TAG_NORMAL = "<td align = \"right\"><span style=\" font-size:8pt; color:grey;\">";
const QString OutputManager::SECTION_COMPONENT_DATEORTIME_END_TAG_NORMAL = "</td></span></tr></table><br>";
const QString OutputManager::SECTION_COMPONENT_ONLYONE_BACK_TAG_NORMAL = "</tr></table><br>";

const QString OutputManager::SECTION_COMPONENT_NAME_FRONT_TAG_ADDITION = "<a id = \"newItem\"><table width = \"100%\"><tr><td align = \"left\" width = \"60%\"><span style=\" font-size:10pt; font-weight:600; color: green;\">";
const QString OutputManager::SECTION_COMPONENT_NAME_BACK_TAG_ADDITION = "</td></span>";
const QString OutputManager::SECTION_COMPONENT_DATEORTIME_FRONT_TAG_ADDITION = "<td align = \"right\"><span style=\" font-size:8pt; color:green;\">";
const QString OutputManager::SECTION_COMPONENT_DATEORTIME_END_TAG_ADDITION = "</td></span></tr></table><br></a>";
const QString OutputManager::SECTION_COMPONENT_ONLYONE_BACK_TAG_ADDITION = "</tr></table><br></a>";

const QString OutputManager::SECTION_COMPONENT_NAME_FRONT_TAG_CLASH = "<table width = \"100%\"><tr><td align = \"left\" width = \"60%\"><span style=\" font-size:10pt; font-weight:600; color:red;\">";
const QString OutputManager::SECTION_COMPONENT_NAME_BACK_TAG_CLASH = "</td></span>";
const QString OutputManager::SECTION_COMPONENT_DATEORTIME_FRONT_TAG_CLASH = "<td align = \"right\"><span style=\" font-size:8pt; color:red;\">";
const QString OutputManager::SECTION_COMPONENT_DATEORTIME_END_TAG_CLASH = "</td></span></tr></table><br>";
const QString OutputManager::SECTION_COMPONENT_ONLYONE_BACK_TAG_CLASH = "</tr></table><br>";

const QString OutputManager::SECTION_COMPONENT_NAME_FRONT_TAG_DONE = "<table width = \"100%\"><tr><td align = \"left\" width = \"60%\"><span style=\" font-size:10pt; font-weight:600;\"><s>";
const QString OutputManager::SECTION_COMPONENT_NAME_BACK_TAG_DONE = "</td></span></s>";
const QString OutputManager::SECTION_COMPONENT_DATEORTIME_FRONT_TAG_DONE = "<td align = \"right\"><span style=\" font-size:8pt;\"><s>";
const QString OutputManager::SECTION_COMPONENT_DATEORTIME_END_TAG_DONE = "</td></span></tr></table><br></s>";
const QString OutputManager::SECTION_COMPONENT_ONLYONE_BACK_TAG_DONE = "</tr></table><br>";

OutputManager::OutputManager(vector<vector<CommonEnum::ReturnString>> outputFromEntriesManager){
	_vectOfVectOfStrings = outputFromEntriesManager;
}


OutputManager::~OutputManager(void){
}


QString OutputManager::getFormattedString(){
	convertToOutputStr();
	return _formattedOutput;
}


QString OutputManager::appendOverdueHeaderTags(pair<string,string> entry){
	
	QString appendedHeader;
	assert(entry.first != "");

	appendedHeader += OVERDUE_HEADER_TITLE_FRONT_TAG;
	appendedHeader += QString::fromStdString(entry.first);
	appendedHeader += OVERDUE_HEADER_TITLE_BACK_TAG;

	return appendedHeader;
}

QString OutputManager::appendTodayHeaderTags(pair<string,string> entry){
	
	QString appendedHeader;
	assert(entry.first != "");
	assert(entry.second != "");

	appendedHeader = combineTagsForTwoData(TODAY_HEADER_TITLE_FRONT_TAG, entry.first, TODAY_HEADER_TITLE_BACK_TAG, 
										TODAY_HEADER_DATE_FRONT_TAG, entry.second, TODAY_HEADER_DATE_BACK_TAG);
	return appendedHeader;
}


QString OutputManager::appendSectionHeaderTags(pair<string, string> entry){
	
	QString appendedHeader;
	//The name will always be there
	assert(entry.first != "");


	if(entry.second != ""){
		appendedHeader = combineTagsForTwoData(SECTION_HEADER_TITLE_FRONT_TAG,entry.first,SECTION_HEADER_TITLE_BACK_TAG,
												SECTION_HEADER_DATE_FRONT_TAG,entry.second, SECTION_HEADER_DATE_BACK_TAG);
	}else{
		appendedHeader = combineTagsForOneData(SECTION_HEADER_TITLE_FRONT_TAG, entry.first, SECTION_HEADER_TITLE_BACK_TAG,
												SECTION_HEADER_ONLYONE_BACK_TAG);
	}

	return appendedHeader;

}

QString OutputManager::appendNormalComponentTags(pair<string, string> entry){

	QString appendedNormalComponent;
	//The name will always be there
	assert(entry.first != "");

	if(entry.second != ""){

		appendedNormalComponent = combineTagsForTwoData(SECTION_COMPONENT_NAME_FRONT_TAG_NORMAL, entry.first,SECTION_COMPONENT_NAME_BACK_TAG_NORMAL,
														SECTION_COMPONENT_DATEORTIME_FRONT_TAG_NORMAL, entry.second ,SECTION_COMPONENT_DATEORTIME_END_TAG_NORMAL);
	}else{

		appendedNormalComponent = combineTagsForOneData(SECTION_COMPONENT_NAME_FRONT_TAG_NORMAL,entry.first, SECTION_COMPONENT_NAME_BACK_TAG_NORMAL,
														SECTION_COMPONENT_ONLYONE_BACK_TAG_NORMAL);
	}

	return appendedNormalComponent;
}

QString OutputManager::appendAdditionComponentTags(pair<string, string> entry){
	
	QString appendedAdditionComponent;
	//The name will always be there
	assert(entry.first != "");

	if(entry.second != ""){

		appendedAdditionComponent = combineTagsForTwoData(SECTION_COMPONENT_NAME_FRONT_TAG_ADDITION,entry.first,SECTION_COMPONENT_NAME_BACK_TAG_ADDITION,
														SECTION_COMPONENT_DATEORTIME_FRONT_TAG_ADDITION,entry.second,SECTION_COMPONENT_DATEORTIME_END_TAG_ADDITION);
	}else{

		appendedAdditionComponent = combineTagsForOneData(SECTION_COMPONENT_NAME_FRONT_TAG_ADDITION,entry.first,
														SECTION_COMPONENT_NAME_BACK_TAG_ADDITION,SECTION_COMPONENT_ONLYONE_BACK_TAG_ADDITION);
	}

	return appendedAdditionComponent;

}

QString OutputManager::appendClashComponentTags(pair<string, string> entry){
		
	QString appendedClashComponent;
	//The name will always be there
	assert(entry.first != "");

	if(entry.second != ""){

		appendedClashComponent = combineTagsForTwoData(SECTION_COMPONENT_NAME_FRONT_TAG_CLASH, entry.first, SECTION_COMPONENT_NAME_BACK_TAG_CLASH,
														SECTION_COMPONENT_DATEORTIME_FRONT_TAG_CLASH, entry.second, SECTION_COMPONENT_DATEORTIME_END_TAG_CLASH);
	}else{

		appendedClashComponent = combineTagsForOneData(SECTION_COMPONENT_NAME_FRONT_TAG_CLASH,entry.first, SECTION_COMPONENT_NAME_BACK_TAG_CLASH,SECTION_COMPONENT_ONLYONE_BACK_TAG_CLASH);
	}

	return appendedClashComponent;

}
QString OutputManager::appendDoneComponentTags(pair<string, string> entry){
		
	QString appendedDoneComponent;
	//The name will always be there
	assert(entry.first != "");

	if(entry.second != ""){

		appendedDoneComponent = combineTagsForTwoData(SECTION_COMPONENT_NAME_FRONT_TAG_DONE,entry.first, SECTION_COMPONENT_NAME_BACK_TAG_DONE,
														SECTION_COMPONENT_DATEORTIME_FRONT_TAG_DONE,entry.second,SECTION_COMPONENT_DATEORTIME_END_TAG_DONE); 
	}else{

		appendedDoneComponent = combineTagsForOneData(SECTION_COMPONENT_NAME_FRONT_TAG_DONE,entry.first,SECTION_COMPONENT_NAME_BACK_TAG_DONE,
														SECTION_COMPONENT_ONLYONE_BACK_TAG_DONE);
	}

	return appendedDoneComponent;

}

QString OutputManager::convertEachSectionToStr(vector<CommonEnum::ReturnString> section){
	QString sectionOutputString;
	
	if(section[0].format == CommonEnum::DISPLAY_FORMAT_DEFAULT){
		sectionOutputString += appendSectionHeaderTags(section[0].entry);
	}else if(section[0].format == CommonEnum::DISPLAY_FORMAT_TODAY){
		sectionOutputString += appendTodayHeaderTags(section[0].entry);
	}else{
		sectionOutputString += appendOverdueHeaderTags(section[0].entry);
	}
	for(int i = 1; i < section.size(); i++){

		sectionOutputString += appendTagsForEachData(section[i]);
	}

	return sectionOutputString;
}

QString OutputManager::appendTagsForEachData(CommonEnum::ReturnString data){

	QString dataString;

	switch(data.format){
		//addition of items
	case CommonEnum::DISPLAY_FORMAT_NEW:
				dataString += appendAdditionComponentTags(data.entry);
				break;

			//clash of items
	case CommonEnum::DISPLAY_FORMAT_CLASH:
				dataString += appendClashComponentTags(data.entry);
				break;
			
	case CommonEnum::DISPLAY_FORMAT_COMPLETED:
				dataString += appendDoneComponentTags(data.entry);
				break;
				//default format //Put assertion for default here/

	case CommonEnum::DISPLAY_FORMAT_DEFAULT:
				dataString +=appendNormalComponentTags(data.entry);
				break;
	default:
		assert(false);
		break;
	}

	return dataString;

}

QString OutputManager::combineTagsForTwoData(QString nameFrontTag, string entryName, QString nameBackTag, QString dateFrontTag,  string date, QString dateBackTag){

	QString combinedString;
	QString convertedName;
	QString convertedDate;

	convertedName = QString::fromStdString(entryName);
	convertedDate = QString::fromStdString(date);

	combinedString = nameFrontTag + convertedName + nameBackTag + dateFrontTag + convertedDate + dateBackTag;

	return combinedString;
}

QString OutputManager::combineTagsForOneData(QString nameFrontTag, string entryName, QString nameBackTag, QString onlyOneLastTag){

	QString combinedString;
	QString convertedName;

	convertedName = QString::fromStdString(entryName);

	combinedString = nameFrontTag + convertedName + nameBackTag + onlyOneLastTag;

	return combinedString;
}


void OutputManager::convertToOutputStr(){

	for(int i = 0; i < _vectOfVectOfStrings.size(); i++){
		_formattedOutput += convertEachSectionToStr(_vectOfVectOfStrings[i]);
	}

}